/*
 *  Copyright 2020-2021 Li Fei
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.pengshun.modules.api.coal.service.impl;

import com.pengshun.modules.api.coal.domain.Coal;
import com.pengshun.modules.api.coal.domain.CoalMine;
import com.pengshun.modules.api.coal.repository.CoalMineRepository;
import com.pengshun.modules.api.coal.service.dto.CoalDto;
import com.pengshun.modules.api.coal.service.dto.CoalMineDto;
import com.pengshun.modules.api.coal.service.mapstruct.CoalMineMapper;
import com.pengshun.modules.api.mapper.PsMerchantMapper;
import com.pengshun.modules.api.coal.para.MapPara;
import com.pengshun.modules.api.coal.para.PagePara;
import com.pengshun.modules.api.coal.result.CoalMineResult;
import com.pengshun.modules.api.coal.result.MerchantMapResult;
import com.pengshun.modules.api.coal.service.CoalMineService;
import com.pengshun.modules.common.BaseResult;
import com.pengshun.modules.domain.MerchantCoal;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author lifei
 * @date 2020-11-04
 **/
@Service
@RequiredArgsConstructor
public class CoalMineServiceImpl implements CoalMineService {


    private final PsMerchantMapper pmm;

    private final CoalMineMapper cmm;



    @Override
    public BaseResult selectPageList(PagePara para)throws Exception {
        int page = (para.getPage()) * para.getSize();
        List<CoalMineResult> castEntity =pmm.selectMineMouth(page<0?0:page, para.getSize(),para.getName(),para.getType(),para.getSort(),para.getLevel(), para.getProvinceId(),para.getCityId(),para.getDistrictId() );
        return new BaseResult(0,castEntity);
    }

    @Override
    public BaseResult selectCoalMineDetil(Long mineMouthId) {
        //todo 自定义查询 状态为正常的
        CoalMine coal = pmm.findByIdAndStatus(mineMouthId);
        CoalMineDto dto=cmm.toDto(coal);
        return new BaseResult(0,dto);
    }

    @Override
    public BaseResult selectAllMerchantMap(MapPara para) {
        List<MerchantMapResult> list=new ArrayList<>();
        if(para.getDistance()==null){
            list=pmm.selectAllmineMouthMap(para.getLongitude(),para.getLatitude(),para.getName());
        }else {
            list = pmm.selectAllmineMouthMapBy(para.getLongitude(), para.getLatitude(), para.getName(), para.getDistance());
        }
        return new BaseResult(0,list);
    }

    @Override
    public BaseResult selectAlltMap(MapPara para) {
        List<MerchantMapResult> results=new ArrayList<>();
        //所有煤矿
        if(para.getMerchantCategoryId()==0){
            //查全部
            List<MerchantMapResult> lists = pmm.selectAllMerchantMap(para.getLongitude(), para.getLatitude(),null,para.getName());
            List<MerchantMapResult> list = pmm.selectAllmineMouthMap(para.getLongitude(), para.getLatitude(), para.getName());
            results.addAll(list);
            results.addAll(lists);
        }

        if(para.getMerchantCategoryId()!=0) {

            if(para.getMerchantCategoryId()==1){
                List<MerchantMapResult> lists = pmm.selectAllMerchantMap(para.getLongitude(), para.getLatitude(),para.getMerchantCategoryId(),para.getName());
                results.addAll(lists);
            }
            if(para.getMerchantCategoryId()==2){
                List<MerchantMapResult> lists = pmm.selectAllMerchantMap(para.getLongitude(), para.getLatitude(),para.getMerchantCategoryId(),para.getName());
                results.addAll(lists);
            }
            if(para.getMerchantCategoryId()==3){
                List<MerchantMapResult> list = pmm.selectAllmineMouthMap(para.getLongitude(), para.getLatitude(), para.getName());
                results.addAll(list);
            }
        }
        return new BaseResult(0,results);
    }

    @Override
    public BaseResult allCoalMineList() {
        List<CoalMine> coalMines=pmm.findAllCoalMine();
        return new BaseResult(0,coalMines);
    }

    @Override
    public BaseResult coalCategory(Long mineMouthId) {
        List<MerchantCoal> list=pmm.findCoalCategoryByMineMouthId(mineMouthId);
        return new BaseResult(0,list);
    }

    @Override
    public BaseResult coals(Coal coal) {
        List<Coal> coals=pmm.findCoalByMineCategoryId(coal.getMineMouthId(),coal.getCoalCategoryId());
        return new BaseResult(0,coals);
    }

}